# _*_encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime

from django.db import models
from organization.models import CourseOrg, Teacher


#  课程表
class Course(models.Model):
    course_org = models.ForeignKey(CourseOrg, verbose_name='课程机构')
    name = models.CharField(max_length=50, verbose_name="课程名")
    desc = models.CharField(max_length=300, verbose_name="课程描述")
    detail = models.TextField(verbose_name="课程详情")
    is_banner = models.BooleanField(default=False, verbose_name="是否轮播")
    teacher = models.ForeignKey(Teacher, verbose_name="讲师", null=True, blank=True)
    degree = models.CharField(max_length=5,
                              choices=(
                                  ("cj", "初级"),
                                  ("zj", "中级"),
                                  ("gj", "高级")
                              ), verbose_name='难度'
                              )
    learn_time = models.IntegerField(default=0, verbose_name="学习时间(分钟)")
    students = models.IntegerField(default=0, verbose_name="学习人数")
    fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
    image = models.ImageField(
        upload_to="courses/%Y/%m",
        verbose_name="封面图",
        max_length=100)
    click_nums = models.IntegerField(default=0, verbose_name="点击数")
    category = models.CharField(default="后端开发", max_length=20, verbose_name="课程类别")
    tag = models.CharField(default='', max_length=10, verbose_name="课程标签")
    need_known = models.CharField(default='', max_length=300, verbose_name="课程须知")
    teacher_tell = models.CharField(default='', max_length=300, verbose_name="老师告诉你")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "课程"
        verbose_name_plural = verbose_name

    def get_lesson_nums(self):
        # 获取课程章节数   前端页面可直接调用此方法
        return self.lesson_set.all().count()

    def get_learn_users(self):
        # 获取学习这门课程的用户  5个
        return self.usercourse_set.all()[:5]

    def get_course_lesson(self):
        # 获取课程章节所有章节
        return self.lesson_set.all()

    def __unicode__(self):
        return self.name


#  章节
class Lesson(models.Model):
    course = models.ForeignKey(Course, verbose_name="课程")
    name = models.CharField(max_length=100, verbose_name="章节名")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "章节"
        verbose_name_plural = verbose_name

    def get_lesson_video(self):
        # 获取章节的所有视频
        return self.video_set.all()

    def __unicode__(self):
        return self.name


class Video(models.Model):
    lesson = models.ForeignKey(Lesson, verbose_name="章节")
    name = models.CharField(max_length=100, verbose_name="视频名")
    url = models.CharField(default='', max_length=200, verbose_name="访问地址")
    learn_time = models.IntegerField(default=0, verbose_name="学习时间(分钟)")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.name


class CourseResource(models.Model):
    course = models.ForeignKey(Course, verbose_name="课程")
    name = models.CharField(max_length=100, verbose_name="名称")
    download = models.FileField(
        upload_to="course/resource/%Y/%m",
        verbose_name="资源文件",
        max_length=100)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name
